#include <iostream>
#include <string>
#include <vector>
using namespace std;
int judge(vector<string> preorder, vector<string> inorder){
    if (preorder.size()==1)return 1;
    if (preorder.size()==0)return 0;
    string root=preorder[0];
    int length;
    int maxdepth=0;
    for (int i=0;i<inorder.size();i++){
        if (inorder[i]==root){
            length=i;
            break;
        }
    }
    
    vector<string> left_preorder;
    for (int i=1;i<length+1;i++)left_preorder.push_back(preorder[i]);

    vector<string> right_preorder;
    for (int i=length+1;i<preorder.size();i++)right_preorder.push_back(preorder[i]);

    vector<string> left_inorder;
    for (int i=0;i<length;i++)left_inorder.push_back(inorder[i]);

    vector<string> right_inorder;
    for (int i=length+1;i<preorder.size();i++)right_inorder.push_back(inorder[i]);
    
    if(left_preorder.size()!=0){
        
        int left_depth=judge(left_preorder, left_inorder);
        if (left_depth>maxdepth)maxdepth=left_depth;
    }

    if(right_preorder.size()!=0){
       
        int right_depth=judge(right_preorder, right_inorder);
        if (right_depth>maxdepth)maxdepth=right_depth;
    }
    return maxdepth+1;


}
int main(){
    vector<string> preorder;
    vector<string> inorder;

    char c;
    string str1;
    c=getchar();
    while (1){
        if (c!=' '&&c!='\n'){str1=str1+c;}
        if(c==' '){preorder.push_back(str1);str1="";}
        if (c=='\n'){preorder.push_back(str1);str1="";break;}
        c=getchar();
    }
    
    
    c=getchar();
    while (1){
        if (c!=' '&&c!='\n')str1=str1+c;
        if(c==' '){inorder.push_back(str1);str1="";}
        if (c=='\n'){inorder.push_back(str1);str1="";break;}
        c=getchar();
    }
    
    int maxdepth=0;
    if (preorder.size()!=0)maxdepth=judge(preorder,inorder);
    cout<<maxdepth<<endl;

    return 0;
}

